package com.eshop.modules.order.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import com.eshop.utils.FileUtil;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.order.service.dto.YxStoreOrderCartInfoDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.order.service.dto.StoreOrderCartInfoQueryCriteria;
import java.util.Iterator;
import java.util.Collection;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import com.eshop.modules.cart.vo.StoreCartQueryVo;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.order.service.StoreOrderCartInfoService;
import com.eshop.modules.order.domain.StoreOrderCartInfo;
import com.eshop.modules.order.service.mapper.StoreOrderCartInfoMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreOrderCartInfoServiceImpl extends BaseServiceImpl<StoreOrderCartInfoMapper, StoreOrderCartInfo> implements StoreOrderCartInfoService
{
    private final IGenerator generator;
    
    public StoreOrderCartInfo findByUni(final String unique) {
        final LambdaQueryWrapper<StoreOrderCartInfo> wrapper = (LambdaQueryWrapper<StoreOrderCartInfo>)new LambdaQueryWrapper();
        wrapper.eq(StoreOrderCartInfo::getUnique, unique);
        return (this.baseMapper).selectOne((Wrapper)wrapper);
    }
    
    public void saveCartInfo(final Long oid, final List<StoreCartQueryVo> cartInfo) {
        final List<StoreOrderCartInfo> list = new ArrayList<StoreOrderCartInfo>();
        for (final StoreCartQueryVo cart : cartInfo) {
            final StoreOrderCartInfo info = new StoreOrderCartInfo();
            info.setOid(oid);
            info.setCartId(cart.getId());
            info.setProductId(cart.getProductId());
            //设置cartInfo是用json字符串存放的
            info.setCartInfo(JSONObject.toJSON(cart).toString());
            //这个唯一码没啥东西，就普通的UUID
            info.setUnique(IdUtil.simpleUUID());
            list.add(info);
        }
        //批量保存效率高
        this.saveBatch((Collection)list);
    }
    
    public Map<String, Object> queryAll(final StoreOrderCartInfoQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreOrderCartInfo> page = (PageInfo<StoreOrderCartInfo>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)YxStoreOrderCartInfoDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreOrderCartInfo> queryAll(final StoreOrderCartInfoQueryCriteria criteria) {
        return (List<StoreOrderCartInfo>)((StoreOrderCartInfoMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreOrderCartInfo.class, criteria));
    }
    
    public void download(final List<YxStoreOrderCartInfoDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final YxStoreOrderCartInfoDto yxStoreOrderCartInfo : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("订单id", yxStoreOrderCartInfo.getOid());
            map.put("购物车id", yxStoreOrderCartInfo.getCartId());
            map.put("商品ID", yxStoreOrderCartInfo.getProductId());
            map.put("购买东西的详细信息", yxStoreOrderCartInfo.getCartInfo());
            map.put("唯一id", yxStoreOrderCartInfo.getUnique());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreOrderCartInfoServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
